package com.docios.cookbook2.ui.loginPager;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.docios.cookbook2.Dao.AppDatabase;
import com.docios.cookbook2.Dao.DatabaseImpl;
import com.docios.cookbook2.Dao.UserDao;
import com.docios.cookbook2.databinding.ActivityRegisterBinding;
import com.docios.cookbook2.entity.User;
import com.docios.cookbook2.utils.CountDownUtil;

public class RegisterActivity extends AppCompatActivity {
    ActivityRegisterBinding registerBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerBinding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(registerBinding.getRoot());

        initData();
        btnCheckcodeOnClick();
        btnRegisterOnClick();
        btnWechatOnClick();
    }

    private void initData() {
    }

    @SuppressLint("SetTextI18n")
    private void btnCheckcodeOnClick() {

        //计时器初始化
        final CountDownUtil countDownUtil = new CountDownUtil(10000, 1000,
                registerBinding.btnCheckcode);

        registerBinding.btnCheckcode.setOnClickListener(view -> {

            //TODO 获取验证码，生成唯一 flag
            Toast.makeText(this, "功能在做啦 T~T", Toast.LENGTH_SHORT).show();

            //验证码等待计时
            countDownUtil.start();

            //TODO 匹配 flag ，验证码正确 停止计时

        });
    }

    private void btnRegisterOnClick() {
        registerBinding.btnRegister.setOnClickListener(view -> {
            String editPhone = registerBinding.inputPhone.getText().toString();
            String editPwd = registerBinding.inputPassword.getText().toString();
            String editCheckcode = registerBinding.inputCheckcode.getText().toString();
            //实例化数据库
            AppDatabase db = DatabaseImpl.setInstance(getApplicationContext());
            UserDao userDao = db.userDao();

            //TODO 注册格式校验
            if (editPhone.isEmpty()) {
                registerBinding.inputPhoneLayout.setError("请输入手机号");
                return;
            } else {
                registerBinding.inputPhoneLayout.setErrorEnabled(false);
            }

            if (editPwd.isEmpty()) {
                registerBinding.inputPwdLayout.setError("请输入密码");
                return;
            } else {
                registerBinding.inputPwdLayout.setErrorEnabled(false);
            }

            //TODO 验证码校验

            //TODO 服务器注册

            // 注入数据库
            if (userDao.getAllByPhoneU(editPhone).isEmpty()) {
                long id = userDao.insertU(new User(editPhone, editPwd,
                        null, null, null, null, null,null));
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                //传值跳转
                startActivity(new Intent(this, LoginActivity.class)
                        .putExtra("phone", editPhone)
                        .putExtra("pwd", editPwd));
                finish();

            } else {
                Toast.makeText(this, "注册过啦", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void btnWechatOnClick() {
        registerBinding.btnWechat.setOnClickListener(view -> {
            //TODO 跳转微信获取用户名，头像
            Toast.makeText(this, "功能在做啦 T~T", Toast.LENGTH_SHORT).show();
        });
    }
}